//
//  LRCellTopView.swift
//  LRWeibo
//
//  Created by 雷路荣 on 15/12/20.
//  Copyright © 2015年 leilurong. All rights reserved.
//

import UIKit

import SDWebImage
///微博cell顶部视图
class LRCellTopView: UIView {

    // 顶部视图显示内容需要模型
    var statues: LRStatues? {
        
        didSet{
            //显示头像 如果有图片就下载显示
            if let profile_image_url = statues?.user?.profile_image_url {
                let url = NSURL(string: profile_image_url)
                iconImageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            //用户昵称
            nickNameLabel.text = statues?.user?.screen_name
            //时间
            timeLabel.text = NSDate.sinaDateToDate(statues!.created_at!).sinaDateDescription()//statues?.user?.created_at
            //来源
            sourceLabel.text = statues?.source
            //vip图标
            vipImageView.image = statues?.user?.mbrankImage
            //认证图标
            verifiedImageView.image = statues?.user?.verifiedImage
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK - 重写系统方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    //MARK - 添加子控件
    private func prepareUI(){
        addSubview(topView)
        addSubview(iconImageView)
        addSubview(nickNameLabel)
        addSubview(vipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedImageView)
        //设置约束
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: self, size: CGSizeMake(UIScreen.mainScreen().bounds.width, 8))
        iconImageView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: CGSizeMake(35, 35), offset: CGPointMake(8, 8))
        nickNameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconImageView, size: nil, offset: CGPointMake(8, 0))
        vipImageView.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: nickNameLabel, size: CGSizeMake(14, 14), offset: CGPointMake(8, 0))
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconImageView, size: nil, offset: CGPointMake(8, 0))
        sourceLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: timeLabel, size: nil, offset: CGPointMake(8, 0))
        verifiedImageView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconImageView, size: CGSizeMake(16, 16), offset: CGPointMake(8, 8))
        
    }
    //MARK - 懒加载
    ///1 顶部视图
    private lazy var topView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(white: 0.95, alpha: 1)
        return view
    }()
    ///2 用户头像
    private lazy var iconImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar"))
        imageView.layer.cornerRadius = 17.5
        imageView.layer.masksToBounds = true
        return imageView
    }()
    ///3 用户名称
    private lazy var nickNameLabel: UILabel = UILabel(fontSize: 16, textColor: UIColor.darkGrayColor())
    ///4 微博时间
    private lazy var timeLabel: UILabel = UILabel(fontSize: 10, textColor: UIColor.orangeColor())
    ///5 微博来源
    private lazy var sourceLabel: UILabel = UILabel(fontSize: 10, textColor: UIColor.lightGrayColor())
    ///6 会员等级图标
    private lazy var vipImageView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    ///7 认证图标
    //private lazy var verifiedImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_grassroot"))
    private lazy var verifiedImageView:UIImageView = UIImageView(image: nil)
}
